var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if (e.id) {
      this.setData({
        id: e.id
      })
    }
    this.loadData();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  loadData: function () {
    var that = this;
    util.postData("/zhc/datalist/getDataByListId", {
      "list_id": "A7367147-930A-47A8-AACC-62DD5B70D9C5",
      "page": {
        "size": 1,
        "current": 1,
        "orders": [
          {
            "column": "a.ADD_TIME",
            "asc": "false"
          }
        ]
      },
      "data": {
        "field": ""
      },
      "repeat_field": [],
      "condition": " ID='"+that.data.id+"' ",
      "virtual_field": []
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success && res.data?.result?.records.length > 0) {
        var dataList = res.data.result.records[0];
        that.setData({
          "addressComponent.province": dataList.province,
          "addressComponent.city": dataList.city,
          "addressComponent.area": dataList.area
        });
        that.setData(dataList);
      }
    });
  },
  setProject: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse?user=shop&warehouse=' + that.data.warehouse
    })
  },
  selectCustomer: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/customerList'
    })
  },
  backToList: function () {
    wx.redirectTo({
      url: '/pages/enterprise/shop'
    })
  },
  formSubmit: function () {
    var that = this;
    let data = {
      "formId":"DC926173-9649-4407-8CE0-270D5F3C5E09",
      "list_id":"",
      "data":[
          {
              "dataId":that.data.ID,
              "target":{
                'name': that.data.name,
                'address': that.data.address,
                'coord': that.data.coord,
                'receive_time': that.data.receive_time,
                'manager': that.data.manager,
                'warehouse': that.data.warehouse||'',
                'customer': that.data.customer||'',
                'tel': that.data.tel,
                'province': that.data.addressComponent.province,
                'city': that.data.addressComponent.city,
                'area': that.data.addressComponent.area
              }
          }],
      "condition":""
      }
    util.postData("/sys/form/commonUpdate", data, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        wx.showToast({
          title: "保存成功",
          icon: "success",
          duration: 3000
        });
      } else {
        wx.showToast({
          title: "保存失败",
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  showLocation:function(){
    var that = this;
    var coord = that.data.coord
    if (coord) {
      coord = coord.split(',');
      wx.openLocation({
        latitude: Number(coord[0]),
        longitude: Number(coord[1]),
        scale: 18
      })
    } else {
      wx.showToast({
        title: "请先选择位置",
        image: '/images/wrong.png',
        duration: 3000
      });
    }
  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        that.setData({
          coord: latitude + ',' + longitude,
          address: that.data.address ? that.data.address : res.address,
          name: that.data.name ? that.data.name : res.name
        })
        util.getData("/xypapi/map/routerCalculate/geocode", {
          latitude: res.latitude,
          longitude: res.longitude
        }, function (res, error) {
          console.log(res); console.log(error)
          if (res.data.success) {
            that.setData({
              "addressComponent.province": res.data.component.province,
              "addressComponent.city": res.data.component.city,
              "addressComponent.area": res.data.component.district
            });
          }
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'path_customer',
      success: function (res) {
        console.log(res)
        if (res.data) {
          that.setData({
            customerName: res.data.name,
            customer:res.data.value
          });
          wx.removeStorage({
            key: 'path_customer',
            success: function (res) {
            }
          });
        }
      }
    });
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          var data = res.data;
          var warehouse = [],wh_name = [];
          for (var i = 0; i < data.length;i++){
            warehouse.push(data[i].value);
            wh_name.push(data[i].name);
          }
          that.setData({
            warehouse: warehouse.join(','),
            wh_name: wh_name.join(',')
          });
          wx.removeStorage({
            key: 'monitor_warehouse',
            success: function (res) {
            }
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})